package com.songlanyun.modules.workPolicy.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.user.entity.SysUserEntity;
import com.songlanyun.modules.user.service.SysUserService;
import com.songlanyun.modules.workPolicy.dao.WorkPolicyDao;
import com.songlanyun.modules.workPolicy.entity.WorkPolicy;
import com.songlanyun.modules.workPolicy.service.WorkPolicyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("workPolicyService")
public class WorkPolicyServiceImpl extends ServiceImpl<WorkPolicyDao, WorkPolicy> implements WorkPolicyService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String header = MapUtil.getStr(params, "header");
        Boolean isShow = MapUtil.getBool(params, "is_show");

        IPage<WorkPolicy> page = this.page(
                new Query<WorkPolicy>().getPage(params),
                new LambdaQueryWrapper<WorkPolicy>()
                        .like(ObjectUtil.isNotNull(header), WorkPolicy::getHeader, header)
                        .eq(ObjectUtil.isNotNull(isShow), WorkPolicy::getIsShow, isShow)
                        .orderByAsc(WorkPolicy::getSort)
                        .orderByDesc(WorkPolicy::getCreateTime));
        return new PageUtils(page);
    }

    @Override
    public WorkPolicy loadById(Integer id, boolean hasThrow) {
        WorkPolicy workPolicy = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(workPolicy) && hasThrow) {
            throw new RRException("未找到该创业政策");
        }
        return workPolicy;
    }

    @Override
    public WorkPolicy add(WorkPolicy workPolicy) {
        this.verifyHeader(workPolicy);
        this.baseMapper.insert(workPolicy);
        return workPolicy;
    }

    @Override
    public WorkPolicy update(WorkPolicy workPolicy) {
        this.verifyHeader(workPolicy);
        this.baseMapper.updateById(workPolicy);
        return workPolicy;
    }

    @Override
    public WorkPolicy info(Integer id) {
        WorkPolicy workPolicy = this.baseMapper.selectOne(
                new LambdaQueryWrapper<WorkPolicy>()
                        .eq(WorkPolicy::getId, id)
                        .eq(WorkPolicy::getIsShow, true)
        );
        if (ObjectUtil.isNull(workPolicy)) {
            throw new RRException("未找到该创业政策");
        }
        //查询发布者
        SysUserEntity user = sysUserService.getById(workPolicy.getCreateBy());
        if (ObjectUtil.isNotNull(user)) {
            workPolicy.setPublisher(user.getNickName());
        }
        return workPolicy;
    }


    private void verifyHeader(WorkPolicy workPolicy) {
        WorkPolicy one = this.baseMapper.selectOne(new LambdaQueryWrapper<WorkPolicy>()
                .eq(WorkPolicy::getHeader, workPolicy.getHeader())
                .ne(ObjectUtil.isNotNull(workPolicy.getId()), WorkPolicy::getId, workPolicy.getId()));
        if (ObjectUtil.isNotNull(one)) {
            throw new RRException("政策名称不能重复");
        }
    }
}
